package com.cloudcc.mobile.view.schedule;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.manager.RunTimeManager;
import com.cloudcc.mobile.manager.UrlManager;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.litesuits.android.log.Log;
import com.mypage.adapter.NewTaskAdapter;
import com.mypage.bean.NewTaskBean;
import com.mypage.utils.UtilsShowDialog;

public class NewStateActivity extends Activity {
	private String mEns = RunTimeManager.getInstance().getlanguage();
	public ListView stateList;
	public TextView bar_title_text;
	public Button mBtnBack;
	public NewTaskBean mDdata ;
	public String STATE;
	private UtilsShowDialog showDialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_state);
		showDialog = new UtilsShowDialog(this);
if ("en".equals(mEns)) {
	showDialog.onShowLoadingDialog("loading...");
		}else{
			showDialog.onShowLoadingDialog("正在加载...");
		}


		mBtnBack = (Button) findViewById(R.id.mBtnBack);
		bar_title_text = (TextView) findViewById(R.id.bar_title_text);
		stateList = (ListView) findViewById(R.id.stateList);
		initListener();
		String mIs = getIntent().getStringExtra("m");
		if ("t".equals(mIs)) {
			STATE = "status";
			if ("en".equals(mEns)) {
				bar_title_text.setText(STATE);
			}else{
				bar_title_text.setText("状态");
			}

			
		}else if("e".equals(mIs)){
			STATE = "type";
			if ("en".equals(mEns)) {
				bar_title_text.setText(STATE);
			}else{
				bar_title_text.setText("类型");
			}

			
		}
		getHttp();
	}
    public void initListener(){
    	mBtnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
    }
	public void getHttp() {
		HttpUtils utils = new HttpUtils();
		RequestParams params = new RequestParams();
		params.addBodyParameter("binding", RunTimeManager.getInstance().getServerBinding());
		params.addBodyParameter("serviceName", "getSelectValue");
		params.addBodyParameter("selecttype",STATE);
		Log.d("状态url-------------------------",UrlManager.getInterfaceUrl()+"?binding="+RunTimeManager.getInstance().getServerBinding()
				+"&serviceName=getSelectValue"+"&selecttype=status");
		utils.send(HttpMethod.POST, UrlManager.getInterfaceUrl(), params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub
						showDialog.onDismissLoadingDialog();
						Log.d("状态接口失败-------------------------", arg1);
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						Log.d("状态接口成功-------------------------", arg0.result);
						 mDdata = new Gson().fromJson(arg0.result, NewTaskBean.class);
						 showDialog.onDismissLoadingDialog();
						 if (!mDdata.result) {
							
							 Toast.makeText(NewStateActivity.this, "获取状态失败", Toast.LENGTH_SHORT).show();
						}
						NewTaskAdapter mAdapter = new NewTaskAdapter(NewStateActivity.this,mDdata);
						stateList.setAdapter(mAdapter);
					}

				});
		stateList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.putExtra("name", mDdata.data.get(position).codekey);
                intent.putExtra("nameName", mDdata.data.get(position).codevalue);
				setResult(1, intent);
				finish();
			}
		});

	}
}
